﻿Imports System.Windows.Forms
Imports System.Windows.Interop
Imports System.Windows.Media.Animation

Class MainWindow

#Region "Window Visiblility Behavior"
    Private WithEvents HideStoryboard As Storyboard, ShowStoryboard As Storyboard
    Public windowVisible As Boolean = True, Animating As Boolean = False
    Private WithEvents CheckTimer As New Forms.Timer() With {.Interval = 500, .Enabled = True}

    Private Sub CheckTimer_Tick(sender As Object, e As EventArgs) Handles CheckTimer.Tick
        If Animating Then Return
        Dim desktopVisible As Boolean = MyProperties.isDesktopVisible
        Dim withinCursorRadius As Boolean = MyProperties.isCursorWithinRadius

        If desktopVisible AndAlso Not windowVisible Then
            Animating = True : ShowStoryboard.Begin()
        ElseIf Not desktopVisible AndAlso windowVisible AndAlso Not withinCursorRadius Then
            Animating = True : HideStoryboard.Begin()
        ElseIf Not windowVisible AndAlso MyProperties.isCursorAtEdge Then
            Animating = True : ShowStoryboard.Begin()
        End If
    End Sub
    Private Sub HideStoryboard_Completed(sender As Object, e As EventArgs) Handles HideStoryboard.Completed
        windowVisible = False
        Animating = False
        For Each Window As Object In My.Application.Windows
            If Window.name = "window" Then
                Window.ClosingStoryboard.Begin()
            ElseIf Window.Name = "contextwindow" Then
                Window.CloseWindow()
            End If
        Next
    End Sub

    Private Sub ShowStoryboard_Completed(sender As Object, e As EventArgs) Handles ShowStoryboard.Completed
        windowVisible = True
        Animating = False

    End Sub

#End Region

#Region "Drag & Drop"
    Private isMouseDown As Boolean
    Private _isDragging As Boolean
    Private _startPoint As Point
    Private _realDragSource As UIElement
    Private _dummyDragSource As New UIElement()

    Private Sub PinStackPanel_Drop(sender As Object, e As Windows.DragEventArgs) Handles PinStackPanel.Drop
        If e.Data.GetDataPresent("UIElement") Then
            Dim droptarget As UIElement = TryCast(e.Source, UIElement)
            Dim droptargetIndex As Integer = -1, i As Integer = 0
            For Each element As UIElement In PinStackPanel.Children
                If element.Equals(droptarget) Then
                    droptargetIndex = i
                    Exit For
                End If
                i += 1
            Next
            If droptargetIndex <> -1 Then
                PinStackPanel.Children.Remove(_realDragSource)
                PinStackPanel.Children.Insert(droptargetIndex, _realDragSource)
            End If

            isMouseDown = False
            _isDragging = False
            _realDragSource.ReleaseMouseCapture()
        ElseIf e.Data.GetDataPresent(DataFormats.FileDrop) Then
            Dim LowestPin As Integer = 70 : For Each Pin As PinControl In PinStackPanel.Children : LowestPin += Pin.ActualHeight : Next
            If e.GetPosition(Me).Y > LowestPin Then
                Dim files As String() = e.Data.GetData(DataFormats.FileDrop)

                For Each Filename As String In files
                    Dim newPin As New PinControl(Filename)
                    PinStackPanel.Children.Add(newPin)
                    Properties.AddPin(newPin.PinData)
                Next
            End If
        End If
    End Sub

    Private Sub PinStackPanel_DragEnter(sender As Object, e As Windows.DragEventArgs) Handles PinStackPanel.DragEnter
        If e.Data.GetDataPresent(DataFormats.FileDrop) Then
            e.Effects = Windows.DragDropEffects.Copy
        ElseIf e.Data.GetDataPresent("UIElement") Then
            e.Effects = DragDropEffects.Move
        End If
    End Sub

    Private Sub PinStackPanel_PreviewMouseLeftButtonDown(sender As Object, e As MouseButtonEventArgs) Handles PinStackPanel.PreviewMouseLeftButtonDown
        If e.Source IsNot PinStackPanel Then
            isMouseDown = True
            _startPoint = e.GetPosition(PinStackPanel)
        End If
    End Sub

    Private Sub PinStackPanel_PreviewMouseLeftButtonUp(sender As Object, e As MouseButtonEventArgs) Handles PinStackPanel.PreviewMouseLeftButtonUp
        isMouseDown = False
        _isDragging = False
        Try : _realDragSource.ReleaseMouseCapture() : Catch : End Try
    End Sub

    Private Sub PinStackPanel_PreviewMouseMove(sender As Object, e As Input.MouseEventArgs) Handles PinStackPanel.PreviewMouseMove
        If isMouseDown Then
            If (_isDragging = False) AndAlso ((Math.Abs(e.GetPosition(PinStackPanel).X - _startPoint.X) > SystemParameters.MinimumHorizontalDragDistance) OrElse (Math.Abs(e.GetPosition(PinStackPanel).Y - _startPoint.Y) > SystemParameters.MinimumVerticalDragDistance)) Then
                _isDragging = True
                _realDragSource = TryCast(e.Source, UIElement)
                _realDragSource.CaptureMouse()
                DragDrop.DoDragDrop(_dummyDragSource, New DataObject("UIElement", e.Source), DragDropEffects.Move)
            End If
        End If
    End Sub

#End Region

    Public Sub New()
        ' This call is required by the designer.
        InitializeComponent()
        ' Add any initialization after the InitializeComponent() call.
        Me.Top = 0
        Me.Left = 0
        Me.Height = MyProperties.ScreenBounds.Height
        Me.HideStoryboard = DirectCast(FindResource("hStoryboard"), Storyboard)
        Me.ShowStoryboard = DirectCast(FindResource("sStoryboard"), Storyboard)
    End Sub

    Private Sub MainWindow_Loaded(sender As Object, e As RoutedEventArgs) Handles Me.Loaded
        MainThread.Start()

        'Load our pins
        For Each P As PinData In Properties.MyPins.Pins
            Dim newPin As New PinControl(P)
            PinStackPanel.Children.Add(newPin)
        Next

    End Sub

End Class
